package com.khy.controller;


import com.khy.pojo.UserInfo;
import com.khy.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("sys")
public class AdminController {
    @Autowired
    private  AdminService adminService;


    @RequestMapping("index")
    @ResponseBody
    public List<UserInfo> selet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<UserInfo> data = adminService.selectt();
          System.out.println("aA+"+data);
          return  data;
    }
/*

    @RequestMapping("my")
    public String druid(){

        return "my";
    }
*/



    @RequestMapping("cha")
    @ResponseBody
    public UserInfo aa(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        UserInfo a = new UserInfo(name,pwd);
        UserInfo u = adminService.select(a);
        System.out.println(name+pwd);
        System.out.println(u+"SSa");
        return u;
    }


    @RequestMapping("delete")
    public  String delete(HttpServletRequest request, HttpServletResponse response){
        String id =request.getParameter("id");
        int id2 = Integer.parseInt(id);

        int a = adminService.delete(id2);
        if(a>0){
            return  "my";
        }
        return  null;
    }


    @RequestMapping("add")
    public  String add(HttpServletRequest request, HttpServletResponse response){
        String name =request.getParameter("name");
        String pwd =request.getParameter("mima");
        UserInfo u = new UserInfo(name,pwd);
        int a = adminService.add(u);
        if(a>0){
            return  "my";
        }

        return  null;
    }


    @RequestMapping("gai")
    @ResponseBody
    public  UserInfo gai(HttpServletRequest request, HttpServletResponse response){
        String id =request.getParameter("id");
        System.out.println(id+"assa");
        int id2 = Integer.parseInt(id);
        UserInfo u = adminService.se(id2);
        System.out.println(u);
         return   u;
    }


        @RequestMapping("gai2")
        public  String gai2(HttpServletRequest request, HttpServletResponse response){
            String id = request.getParameter("id");
            int i = Integer.parseInt(id);
            String name = request.getParameter("name");
            String pwd = request.getParameter("pwd");
            int a = adminService.update( new UserInfo(i,name,pwd));
            if(a>0){
                return  "my";
            }
            return  null;
        }
}
